package com.lbj.googleplay.googleplay100.ui.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import com.lbj.googleplay.googleplay100.R;
import com.lbj.googleplay.googleplay100.manager.ThreadManager;
import com.lbj.googleplay.googleplay100.utils.UIUtils;

/*
* 根据当前状态来显示不同页面
* -未加载
* -加载中
* -加载失败
* -数据为空
* -加载成功
*
* */
public abstract class LoadingPage extends FrameLayout {

    private static final int STATE_LOAD_UNDO = 1;//未加载
    private static final int STATE_LOAD_LOADING = 2;//加载中
    private static final int STATE_LOAD_ERROR = 3;//加载失败
    private static final int STATE_LOAD_EMPTY= 4;//数据为空
    private static final int STATE_LOAD_SUCCESS = 5;//加载成功

    private  int mCurrentState = STATE_LOAD_UNDO;//当前状态
    private View mLoadingPage;
    private View mErrorPage;
    private View mEmptyPage;
    private View mSuccessPage;

    public LoadingPage(@NonNull Context context) {
        super(context);
        initView();

    }

    public LoadingPage(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();

    }

    public LoadingPage(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        //初始化加载中布局
        if (mLoadingPage==null) {
            mLoadingPage = UIUtils.inflate(R.layout.page_loading);
            addView(mLoadingPage);
        }
        //初始化加载失败布局
        if (mErrorPage==null) {
            mErrorPage = UIUtils.inflate(R.layout.page_error);
            Button btn_load_retry = mErrorPage.findViewById(R.id.btn_load_retry);
            btn_load_retry.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //重新加载数据
                    loadData();
                }
            });
            addView(mErrorPage);
        }
        //初始化数据为空布局
        if (mEmptyPage==null){
            mEmptyPage = UIUtils.inflate(R.layout.page_empty);
            addView(mEmptyPage);
        }

        //根据当前状态决定显示哪个布局
        showRightPage();

    }
    //开始加载数据
    public void loadData(){

        if (mCurrentState != STATE_LOAD_LOADING) {// 如果当前没有加载, 就开始加载数据

            mCurrentState = STATE_LOAD_LOADING;

//            new Thread() {
//                @Override
//                public void run() {
//                    final ResultState resultState = onLoad();
//                    // 运行在主线程
//                    UIUtils.runOnUIThread(new Runnable() {
//
//                        @Override
//                        public void run() {
//                            if (resultState != null) {
//                                mCurrentState = resultState.getState();// 网络加载结束后,更新网络状态
//                                // 根据最新的状态来刷新页面
//                                showRightPage();
//                            }
//                        }
//                    });
//                }
//            }.start();

            ThreadManager.getThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    final ResultState resultState = onLoad();
                    UIUtils.runOnUIThread(new Runnable() {
                        @Override
                        public void run() {
                            // 运行在主线程
                    UIUtils.runOnUIThread(new Runnable() {

                        @Override
                        public void run() {
                            if (resultState != null) {
                                mCurrentState = resultState.getState();// 网络加载结束后,更新网络状态
                                // 根据最新的状态来刷新页面
                                showRightPage();
                            }
                        }
                    });
                        }
                    });

                }
            });
        }
    }
    //根据当前状态决定显示哪个布局
    private void showRightPage() {

        mLoadingPage.setVisibility((mCurrentState==STATE_LOAD_UNDO||mCurrentState==STATE_LOAD_LOADING)?View.VISIBLE:View.GONE);

        mErrorPage.setVisibility(mCurrentState==STATE_LOAD_ERROR?View.VISIBLE:View.GONE);

        mEmptyPage.setVisibility(mCurrentState==STATE_LOAD_EMPTY?View.VISIBLE:View.GONE);

        //当成功布局为空并且当前状态为成功,才初始化成功的布局
        if (mSuccessPage==null && mCurrentState==STATE_LOAD_SUCCESS){

            mSuccessPage = onCreateSuccessView();

            if (mSuccessPage!=null){
                addView(mSuccessPage);
            }
        }

        if (mSuccessPage != null) {
            mSuccessPage
                    .setVisibility(mCurrentState == STATE_LOAD_SUCCESS ? View.VISIBLE
                            : View.GONE);
        }

    }
    //加载成功后需要的显示布局,交给子类来实现
    public abstract View onCreateSuccessView();
    //加载数据方法,子类实现,返回值表示请求网络结束后的状态
    public abstract ResultState onLoad();

   //用来表示网络请求返回结果的枚举
   public enum  ResultState{

         STATE_SUCCESS(STATE_LOAD_SUCCESS),
         STATE_ERROR(STATE_LOAD_ERROR),
         STATE_EMPTY(STATE_LOAD_EMPTY);

         private int state;

         private ResultState(int state){
           this.state = state;
         }
         public int getState(){
           return state;
         }

   }
}
